package kyFtp.process
{
	import flash.events.Event;
	import flash.events.OutputProgressEvent;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	import kyFtp.events.FtpProgressEvent;
	import kyFtp.events.HandleFileEvent;
	import kyFtp.request.FtpRequest;
	import kyFtp.request.FtpRequestType;
	import kyFtp.response.Response;
	import kyFtp.response.ResponseType;
	import kyFtp.utils.ExtendSocket;
	import kyFtp.utils.ProgressData;

	/**
	 * @author 秦家洋
	 * @E-mail: develop@kongyue.cc
	 * 创建时间：2015-7-2 下午10:18:47
	 * 
	 */
	public class UploadProcess extends ProcessBase
	{
		private var file:File;
		private var fs:FileStream;
		private var fileSize:Number;
		
		private var timer:Timer;
		private var bufferSize:int = 1024 * 1024 * 10;
		private var serverFileName:String;
		
		private var pasvSock:ExtendSocket;
		private var isRead:Boolean = true;
		private var oldPeding:Number=0;
		private var old2Peding:Number=0;
		private var total:Number=0;
		private var progress:Number;
		private var pd:ProgressData = new ProgressData();
		public function UploadProcess(file:File,serverFileName:String)
		{
			this.file = file;
			this.serverFileName = serverFileName;
			fileSize = file.size;
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER,runTimer);
		}
		public override function responseHandler(res:Response):void
		{
			switch(res.code)
			{
				case ResponseType.COMMAND_OK:
					this.sendCmd(FtpRequest.parse(FtpRequestType.PASV));
					break;
				case ResponseType.ENTERING_PASV:
					pasvSock = ExtendSocket.parsePASVSock(res.message);
					pasvSock.addEventListener(Event.CONNECT, onPasvConnected);
					pasvSock.addEventListener(ProgressEvent.SOCKET_DATA, onPasvData);
					pasvSock.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS,outPutData);
					break;
				case ResponseType.DATA_CONN_CLOSE:
					dispatchEvent(new HandleFileEvent(HandleFileEvent.UPLOAD_SUCCESS));
					break;
				case ResponseType.FILE_STATUS_OK:
					startSendingData();
					break;
				default:
					//nothing
			}
		}
		override public function sendRequest():void
		{
			sendCmd(FtpRequest.parse(FtpRequestType.BINARY));
		}
		private function runTimer(e:TimerEvent):void
		{
			if(isRead)
			{
				isRead = false;
				if(fs.bytesAvailable <= 0)
				{
					timer.stop();	
					clearSocket();
				}
				else
				{
					var by:ByteArray = new ByteArray();
					try
					{
						fs.readBytes(by, 0, bufferSize);
					}
					catch(e:Error)
					{
						fs.readBytes(by);
					}
					pasvSock.writeBytes(by);
					pasvSock.flush();
					
				}
			}
			old2Peding = total;
			var speed:Number = Math.floor((old2Peding - oldPeding)/1024);
			oldPeding = total;
			
			pd.total = fileSize;
			pd.speed = speed;
			pd.progress = progress;
			dispatchEvent(new FtpProgressEvent(FtpProgressEvent.PROGRESS,pd));
			
		}
		private function startSendingData():void
		{
			fs = new FileStream();
			fs.open(file, FileMode.READ);
			timer.start();
		}
		private function onPasvConnected(event:Event):void
		{
			this.sendCmd(FtpRequest.parse(FtpRequestType.STOR, serverFileName));
		}
		
		private function onPasvData(event:ProgressEvent):void
		{
			
		}
		private function outPutData(e:OutputProgressEvent):void
		{
			var n1:Number = e.bytesPending;
			total = e.bytesTotal;
			progress = Math.floor((total/fileSize)*10000)/100;
			pd.loaded = total;
			if(n1<=0)
				isRead = true;
		}
		private function clearSocket():void
		{
			pasvSock.close();
			pasvSock.removeEventListener(Event.CONNECT, onPasvConnected);
			pasvSock.removeEventListener(ProgressEvent.SOCKET_DATA, onPasvData);
			pasvSock.removeEventListener(OutputProgressEvent.OUTPUT_PROGRESS,outPutData);
			pasvSock = null;
		}
	}
}